<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//注册控制器
class RegController extends Controller{
	private $userModel;
	//构造函数
	public function __init()
	{
		$this->userModel = new \Common\Model\User;
	}
	
	public function code(){
//  	验证码
		Code::num(4)->fontSize(14)->width(104)->height(44)->make();
//		存验证码
	}
    //动作
    public function index(){
    	if(IS_POST){
    		if(!$this->userModel->reg()) View::error($this->userModel->getError());
			View::success('注册成功，跳转到登录页面',U('Login/index'));
    	}
       	View::make();
    }
//	注册验证
	public function regajax(){
//		1.用户名验证
		if(Q('post.username')){
			$this->exist('username',Q('post.username'));
		}
		if(Q('post.email')){
			$this->exist('email',Q('post.email'));
		}
		if(Q('post.mcode')){
			$this->mcode(Q('post.mcode'));
		}
		
	}
//	验证存在
	public function exist($name,$data){
		$status = $this->userModel->where("{$name} = '{$data}'")->find();
		if($status){
			echo json_encode(array('status'=>FALSE));die;
		}else{
			echo json_encode(array('status'=>TRUE));die;
		}
	}
//	验证码验证
	public function mcode($mcode){
		if(strtoupper($mcode) != Code::get()){
			echo json_encode(array('status'=>FALSE));die;
		}else{
			echo json_encode(array('status'=>TRUE));die;
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
